<template>
  <div class="container">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-position="top"
    >
      <el-form-item label="登录方式" prop="loginMethod">
        <el-select v-model="ruleForm.loginMethod" placeholder="请选择登录方式">
          <el-option label="手机号" value="iphone"></el-option>
          <el-option label="身份证号" value="idcard"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="手机号"
        prop="iphone"
        v-if="ruleForm.loginMethod === 'iphone'"
      >
        <el-input v-model="ruleForm.iphone"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idcard" v-else>
        <el-input v-model="ruleForm.idcard"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        loginMethod: "iphone",
        idcard: "",
        iphone: "",
        password: ""
      },
      rules: {
        loginMethod: [
          { required: true, message: "请选择登录方式", trigger: "change" }
        ],
        iphone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "长度在11个字符", trigger: "blur" }
        ],
        idcard: [
          { required: true, message: "请输入身份证号", trigger: "blur" },
          { min: 18, max: 18, message: "长度在18个字符", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          this.$message.error("用户名或密码错误！");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.el-form {
  width: 500px;
  margin: 130px auto;
  border-radius: 10px;
  border: 1px solid #ccc;
  box-shadow: 0 0 30px #ccc;

  padding: 30px;
}
.el-form-item {
  margin: 0 auto;
  width: 400px;
  margin-bottom: 20px;
}
.el-select {
  width: 100%;
}
</style>
